package com.oauth.global.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.ui.HorizontalFlowPanel;
import com.oauth.global.client.beans.AppInfoBean;
import com.oauth.global.client.beans.BirthdayClientBean;
import com.oauth.global.client.rpc.GlobalRemoteServiceAsync;

public class BirthdayPanel extends Composite {

	private static BirthdayPanelUiBinder uiBinder = GWT.create(BirthdayPanelUiBinder.class);

	interface BirthdayPanelUiBinder extends UiBinder<Widget, BirthdayPanel> {
	}

	@UiField
	Image appImage;

	@UiField
	Label appName;

	@UiField
	HorizontalFlowPanel memberPanel;

	@UiField
	DeckPanel deckPanel;

	GlobalRemoteServiceAsync service;

	AppInfoBean appInfoBean;

	public BirthdayPanel(AppInfoBean pAppInfoBean, GlobalRemoteServiceAsync service) {
		initWidget(uiBinder.createAndBindUi(this));
		this.service = service;
		appInfoBean = pAppInfoBean;
		appImage.setUrl(pAppInfoBean.getImageUrl());
		appName.setText(pAppInfoBean.getName());
	}

	public void initBirthdays() {
		deckPanel.showWidget(0);
		service.getUpcomingBirthdays(appInfoBean.getId(),
				new OAuthAsnyCallBack<List<BirthdayClientBean>>() {

					@Override
					public void onSuccess(List<BirthdayClientBean> pbirthdayList) {
						if (pbirthdayList == null || pbirthdayList.isEmpty()) {
							memberPanel.add(new HTML("No upcoming birthday's"));
						} else {
							for (BirthdayClientBean birthday : pbirthdayList) {
								memberPanel.add(new Birthday(birthday));
							}
						}
						deckPanel.showWidget(1);
					}
				});
	}
}
